# coding: utf-8
import logging

from django import forms
from django.contrib.auth import authenticate, login as auth_login

from models import User

class RegisterForm(forms.Form):
    email = forms.EmailField(label=u'邮箱')
    password = forms.CharField(label=u'密码', widget=forms.PasswordInput)
    name = forms.CharField(label=u'姓名')

    def clean_email(self):
        email = self.cleaned_data['email']
        user = User.all().filter('email = ', email).get()
        if user:
            raise forms.ValidationError(u'邮箱 %s 已经注册' % email)
        return email

    def register(self):
        user = User.create_user(email=self.cleaned_data['email'],
                        password=self.cleaned_data['password'],
                        name = self.cleaned_data['name'])
        return user

class LoginForm(forms.Form):
    email = forms.EmailField(label=u'邮箱')
    password = forms.CharField(label=u'密码', widget=forms.PasswordInput)

    def clean(self):
        email, password = self.cleaned_data.get('email'), self.cleaned_data.get('password')
        if email and password:
            user = authenticate(username=email, password=password)
            if user is None:
                raise forms.ValidationError(u'邮箱或密码错误')
        return self.cleaned_data

    def login(self, request):
        logging.info('emai: %s' % self.cleaned_data['email'])
        email, password = self.cleaned_data['email'], self.cleaned_data['password']
        user = authenticate(username=email, password=password)
        if user is not None:
            auth_login(request, user)
            return user
        else:
            return None

class ProfileForm(forms.Form):
    name = forms.CharField(label=u'姓名')
    
    def __init__(self, instance, *args, **kwargs):
        self.user = instance
        if self.user:
            initial = kwargs.setdefault('initial', {})
            initial.setdefault('name', self.user.name)
        super(ProfileForm, self).__init__(*args, **kwargs)

    def save(self):
        user = self.user
        user.name = self.cleaned_data['name']
        user.put()

class ChangePasswordForm(forms.Form):
    old_password = forms.CharField(label=u'旧密码', widget=forms.PasswordInput)
    password = forms.CharField(label=u'新密码', widget=forms.PasswordInput)
    password_confirmed = forms.CharField(label=u'确认密码', widget=forms.PasswordInput)

    def __init__(self, instance, *args, **kwargs):
        self.user = instance
        super(ChangePasswordForm, self).__init__(*args, **kwargs)

    def clean_old_password(self):
        old_password = self.cleaned_data['old_password']
        if not self.user.check_password(old_password):
            raise forms.ValidationError(u'旧密码不正确')
        return old_password

    def clean_password(self):
        password = self.cleaned_data['password']
        if 'password_confirmed' not in self.cleaned_data:
            return password
        password_confirmed = self.cleaned_data['password_confirmed']
        if password != password_confirmed:
            raise forms.ValidationError(u'两次密码不匹配')
        return password

    def change_password(self):
        self.user.set_password(self.cleaned_data['password'])
        self.user.put()
        return self.user

